﻿using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace GyroTestApp
{
    public partial class Program
    {
        GTM.Seeed.Gyro gyro = new GTM.Seeed.Gyro(3);

        void ProgramStarted()
        {

            gyro.MeasurementComplete += new GTM.Seeed.Gyro.MeasurementCompleteEventHandler(gyro_MeasurementComplete);
            
            //gyro.Calibrate();
            //gyro.SaveCalibration();

            //if (gyro.LoadCalibration())
            //{
            //    Debug.Print("Calibration loaded.");
            //}

            // gyro.LowPassFilter = GTM.Seeed.Gyro.Bandwidth._5Hz;
            // gyro.SampleRateDivider = 255;

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Use StartContinousMeasurements to sample the gyro periodically, at a rate specified by the ContinousMeasurementInterval
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            // gyro.ContinuousMeasurementInterval = 100; // 100 ms
            // gyro.StartContinuousMeasurements();

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Alterntatively, handle the DataReady event to request a measurement only
            // when new data is availble. This avoids reading duplicate data, or missing a data measurement.
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            gyro.DataReady += new GTM.Seeed.Gyro.DataReadyHandler(gyro_DataReady);
            gyro.RequestMeasurement();

            Debug.Print("Program Started");
        }

        void gyro_DataReady(GTM.Seeed.Gyro sender)
        {
            gyro.RequestMeasurement();
        }

        void gyro_MeasurementComplete(GTM.Seeed.Gyro sender, GTM.Seeed.Gyro.SensorData sensorData)
        {
            Debug.Print(sensorData.ToString());
        }
    }
}
